package com.shop.search.controller;

import com.base.common.http.HttpResult;
import com.shop.search.async.AsyncWork;
//import com.shop.search.http.HttpResult;
import com.shop.search.service.ItemSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/item")
public class ItemSearchController {

    @Autowired
    private ItemSearchService itemSearchService;

    /** 异步把数据库商品信息导入es中的具体操作 */
    @Autowired
    private AsyncWork asyncWork;



    /** typeId=1 获取全部商品模板， typeId=2 获取全部商品品牌 */
    @RequestMapping("/getCommonData")
    public String getCommonData(@RequestParam("typeId") Integer typeId) {
        return itemSearchService.getCommonData(typeId);
    }

    /** 把数据库多表查询的商品信息组合一起，异步方式地保存到es中 */
    @RequestMapping("/importDataToES")
    public void importDataToES() {
        asyncWork.importDataToES();
        System.out.println("服务调用完成，异步执行...");
    }

    /** 联想词查询 */
    @RequestMapping("/searchAssociation")
    public HttpResult searchAssociation(@RequestParam("inputText") String inputText) {
        try {
            Set<String> result = itemSearchService.searchAssociation(inputText);
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResult.error();
        }
    }

    /** 搜索 */
    @RequestMapping("/searchConditionByEs")
    public HttpResult searchAll(@RequestBody Map searchMap){
        return HttpResult.ok(itemSearchService.searchAll(searchMap));
    }
}
